package com.prince.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * List 工具类
 *
 * @author
 * @date 2017/10/13
 */
public class ListUtils {

	public static <T> boolean isNotEmpty(List<T> tList) {
		return !isEmpty(tList);
	}

	public static <T> boolean isEmpty(List<T> tList) {
		return tList == null || tList.isEmpty() || 0 == tList.size();
	}

	public static List<String> integerToString(List<Integer> list) {
		List<String> stringList = new ArrayList<String>();
		if (isNotEmpty(list)) {
			for (Integer integer : list) {
				stringList.add(integer + "");
			}
		}
		return stringList;
	}

	public static void listSort(List<Map<String, String>> list,String sort) {
		Collections.sort(list, new Comparator<Map<String, String>>() {
			@Override
			public int compare(Map<String, String> o1, Map<String, String> o2) {
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				try {
					Date dt1 = format.parse((String) o1.get(sort));
					Date dt2 = format.parse((String) o2.get(sort));
					if (dt1.getTime() > dt2.getTime()) {
						return -1;
					} else if (dt1.getTime() < dt2.getTime()) {
						return 1;
					} else {
						return 0;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return 0;
			}
		});
	}
	public static void ListSort(List<Map<String, Object>> list,String sort) {
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date dt1 = format.parse((String) o1.get(sort));
                    Date dt2 = format.parse((String) o2.get(sort));
                    if (dt1.getTime() > dt2.getTime()) {
                        return -1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return 1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }
	public static List<String> longToString(List<Long> list) {
		List<String> stringList = new ArrayList<String>();
		if (isNotEmpty(list)) {
			for (Long l : list) {
				stringList.add(l + "");
			}
		}
		return stringList;
	}

	public static List<Integer> stringToInteger(List<String> list) {
		List<Integer> integerList = new ArrayList<Integer>();
		if (isNotEmpty(list)) {
			for (String str : list) {
				if (StringUtil.isNotEmpty(str)) {
					integerList.add(Integer.parseInt(str));
				}
			}
		}
		return integerList;
	}

	public static List<Long> stringToLong(List<String> list) {
		List<Long> longList = new ArrayList<Long>();
		if (isNotEmpty(list)) {
			for (String str : list) {
				if (StringUtil.isNotEmpty(str)) {
					longList.add(Long.parseLong(str));
				}
			}
		}
		return longList;
	}

	/**
	 * 分割List
	 *
	 * @param list
	 *            待分割的list
	 * @param pageSize
	 *            每段list的大小
	 * @return List<<List<T>>
	 */
	public static <T> List<List<T>> splitList(List<T> list, int pageSize) {
		// list的大小
		int listSize = list.size();
		// 页数
		int page = (listSize + (pageSize - 1)) / pageSize;
		// 创建list数组 ,用来保存分割后的list
		List<List<T>> listArray = new ArrayList<List<T>>();
		// 按照数组大小遍历
		for (int i = 0; i < page; i++) {
			// 数组每一位放入一个分割后的list
			List<T> subList = new ArrayList<T>();
			// 遍历待分割的list
			for (int j = 0; j < listSize; j++) {
				// 当前记录的页码(第几页)
				int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
				// 当前记录的页码等于要放入的页码时
				if (pageIndex == (i + 1)) {
					// 放入list中的元素到分割后的list(subList)
					subList.add(list.get(j));
				}
				// 当放满一页时退出当前循环
				if ((j + 1) == ((j + 1) * pageSize)) {
					break;
				}
			}
			// 将分割后的list放入对应的数组的位中
			listArray.add(subList);
		}
		return listArray;
	}

	public static <T> void repeatList(List<T> list) {
		Set<T> set = new HashSet<T>(list);
		list.clear();
		list.addAll(set);
	}

	
}
